package com.stone.business.jc.action;

/**
 * <pre>
 * 用户绩效档案
 * @author 
 * </pre>
 */

import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.jc.pojo.TUserJxda;
import com.stone.business.jc.service.JcCommonService;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TUser;
import com.stone.utils.JsonUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.page.Page;

public class UserJxdaAction  extends BaseAction {
	
	private static final long serialVersionUID = -4647594073681054487L;

	private JcCommonService myService;
	
	private String start;
	private String limit;

	public JcCommonService getMyService() {
		return myService;
	}

	public void setMyService(JcCommonService myService) {
		this.myService = myService;
	}
	
	/**
	 * 进入发送列表页面
	 * @return
	 */
	public String initList() {

	   	String today = myService.getSysdate("yyyy-MM-dd");
	   	super.getRequest().setAttribute("today", today);
		return super.SUCCESS;

	}

	/**
	 * 加载历史数据TREE
	 */
	public void getHistoryDate() throws Exception {
		
		String flag = super.getRequest().getParameter("flag");
		
		List<HashMap<String,Object>> listTree;
		
		// 年
		if ("-1".equals(flag)) {
			
			listTree = myService.findListBySql("select to_char(rq,'YYYY') as text, to_char(rq,'YYYY') as id, 0 as leaf from tab_user_jxda group by to_char(rq,'YYYY') order by id");

		// 月
		} else {
			
			listTree = myService.findListBySql("select to_char(rq,'MM') as text, " + flag + " || '-' || to_char(rq,'MM') as id, 1 as leaf from tab_user_jxda where to_char(rq,'YYYY') = '" + flag + "'  group by to_char(rq,'MM') order by id");

		}
			
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(listTree, jsonConfig);
        
        super.getResponse().getWriter().print(json.toString());
	}

	/**
	 * 绩效档案列表
	 * 
	 * @return
	 * @throws AppException
	 */
	public void getJxdaList() throws Exception {
		
		String conditions = "1=1";

		// 是否管理员
		String isAdmin = (String) super.getSession().getAttribute(IConstant.SESSION_ISADMIN);
		long count = 1;
		if (!"1".equals(isAdmin)) {
			Long userid = ((TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId();
			// 是否是工区管理人员
			 count = myService.getTotalByDHql("u.roleId = r.id and r.nameen = 'GLRY' and u.userId = ?", new Object[]{userid}, "TUserRole u, TRole r");
			
			if (count == 0) {
				conditions = "userid = '" + userid.toString() + "'";
			}
		} 
		
		// 历史数据列表按照年月查询
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon10()) && super.getSearchBean().getCon10().length() == 7) {
			conditions += " AND TO_CHAR(rq,'yyyy-MM') =  '" + super.getSearchBean().getCon10() + "'";
		} else if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon10()) && super.getSearchBean().getCon10().length() == 4) {
			conditions += " AND TO_CHAR(rq,'yyyy') =  '" + super.getSearchBean().getCon10() + "'";
		}
		//String years=super.getSearchBean().getCon10();
	//	System.out.println(years);
		/*Page page = new Page();

		page.setStartRs(Integer.parseInt(getStart()));
		page.setPerPage(Integer.parseInt(getLimit()));
		page.setObject(new HashMap());

		StringBuffer sql = new StringBuffer();
		sql.append("select ");
		sql.append("  khxznr as nr, id as id, to_char(rq,'YYYY-MM') as rq, username as username, grjxdf as grjxdf, dadf as dadf, gzldf as gzldf, gzzldf as gzzldf, zxkhdf as zxkhdf, wzbts as wzbts, pxdf as pxdf, ksdf as ksdf, xcbddf as xcbddf, cxcgdf as cxcgdf");
		
//		sql.append("(select ltrim(sys_connect_by_path(khxzremark, '★'),'★') as khxzremark");
//		sql.append(" from (select userid, ");
//		sql.append("           to_char(KHRQ, 'YYYY-MM') as khrq2,KHXZREMARK, count(*) over(partition by userid, to_char(KHRQ, 'YYYY-MM')) cnt ");
//		sql.append("       from TAB_JXKH_GZZLKH d ");
//		sql.append("       order by id) ss ");
//		sql.append(" where level = cnt ");
//		sql.append(" and ss.userid = s.userid ");
//		sql.append(" and khrq2 = TO_CHAR(rq, 'yyyy-MM') ");
//		sql.append(" connect by prior userid = userid and prior khrq2 = khrq2 ");
//		sql.append(" and prior khxzremark < khxzremark) as nr ");
		sql.append("  from tab_user_jxda s");
		sql.append("  where ");
		sql.append(conditions);
		sql.append(" order by s.id");

		// 根据起始记录查询当前页记录
		List<HashMap<String, Object>> list = myService.getPageListBySQL(page, sql.toString());

		JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM");

		JSON json = JSONSerializer.toJSON(list, jsonConfig);

		String strTree = "{result:" + json.toString() + ",totalCount:[" + myService.getTotalBySql(sql.toString()) + "]}";

		super.getResponse().getWriter().print(strTree);
		
//		
//
//		super.setPage((String.valueOf((Integer.parseInt(getStart()) / Integer.parseInt(getLimit()) + 1))));
//		
//		HashMap<String, Object> m = new HashMap<String, Object>();
//		List<Object> valsList = new ArrayList<Object>();
//		
//		String cols = "id as id, to_char(rq,'YYYY-MM') as rq, username as username, grjxdf as grjxdf, dadf as dadf, gzldf as gzldf, gzzldf as gzzldf, zxkhdf as zxkhdf, wzbts as wzbts, pxdf as pxdf, ksdf as ksdf, xcbddf as xcbddf, cxcgdf as cxcgdf";
//
//		String conditions = "1=1 ";
//		
//		// 历史数据列表按照年月查询
//		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon10()) && !"-1".equals(super.getSearchBean().getCon10())) {
//
//		//	valsList.add(super.getSearchBean().getCon10());
//		}
//
//		m.put("cols", cols);
//		m.put("alias", "note");
//		m.put("conditions",conditions);//conditions);
//		m.put("clause", "order by rq desc");
//	//	m.put("vals", valsList.toArray());
//		
//		// 根据起始记录查询当前页记录
//		List<HashMap<String,Object>> list = myService.getPageListByDHql(Integer.parseInt(getStart()), Integer.parseInt(getLimit()) , m, TUserJxda.class.getName() );
//		
//        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd HH:mm:ss");
//    	
//        JSON json = JSONSerializer.toJSON(list, jsonConfig);
//
//		String strTree = "{result:" + json.toString() + ",totalCount:[" + myService.getTotalByDHql(conditions, valsList.toArray(), TUserJxda.class.getName() + ""  ) + "]}";
//		
//        super.getResponse().getWriter().print(strTree);
*/		//List<HashMap<String,Object>> list = myService.getListByDHql("khxznr as nr, id as id, rq as rq, username as username, grjxdf as grjxdf, dadf as dadf, gzldf as gzldf, gzzldf as gzzldf, zxkhdf as zxkhdf, wzbts as wzbts, pxdf as pxdf, ksdf as ksdf, xcbddf as xcbddf, cxcgdf as cxcgdf",
			//	conditions, null, "order by id", TUserJxda.class.getName());
		List<HashMap<String,Object>> list = null;
		
		// 管理员、管理人员
		if (count > 0 && (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon10()) && super.getSearchBean().getCon10().length() == 4)){
			
	//		list = myService.getListByDHql("'" + super.getSearchBean().getCon10() + "' as rq, '' as nr, username as username, sum(grjxdf) as grjxdf, sum(dadf) as dadf, sum(gzldf) as gzldf, sum(gzzldf) as gzzldf, sum(zxkhdf) as zxkhdf, sum(wzbts) as wzbts, sum(pxdf) as pxdf, sum(ksdf) as ksdf, sum(xcbddf) as xcbddf, sum(cxcgdf) as cxcgdf",
	//				conditions, null, "group by username", TUserJxda.class.getName());
	//		
			list = myService.findListBySql("select '" + super.getSearchBean().getCon10() + "' as rq, min(id) as id, '' as nr, username as username, sum(grjxdf) as grjxdf, sum(dadf) as dadf, sum(gzldf) as gzldf, sum(gzzldf) as gzzldf, sum(zxkhdf) as zxkhdf, sum(wzbts) as wzbts, sum(pxdf) as pxdf, sum(ksdf) as ksdf, sum(xcbddf) as xcbddf, sum(cxcgdf) as cxcgdf from tab_user_Jxda "
					+ " where " + conditions + " group by username order by id ");
			
		} else {
			
			list = myService.getListByDHql("khxznr as nr, id as id, rq as rq, username as username, grjxdf as grjxdf, dadf as dadf, gzldf as gzldf, gzzldf as gzzldf, zxkhdf as zxkhdf, wzbts as wzbts, pxdf as pxdf, ksdf as ksdf, xcbddf as xcbddf, cxcgdf as cxcgdf",
					conditions, null, "order by id", TUserJxda.class.getName());
		}
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM");
    	
        JSON json = JSONSerializer.toJSON(list, jsonConfig);

		String strTree = "{result:" + json.toString() + ",totalCount:[" + list.size() + "]}";
		
        super.getResponse().getWriter().print(strTree);
	
	}
	
	/**
	 * 删除
	 * @return
	 * @throws AppException
	 */
	public void delete() throws Exception {
		
		myService.doDelAll(super.getRequest().getParameter("id"), TUserJxda.class.getName());
		
		super.getResponse().getWriter().print("{msg:'删除成功'}");
		
	}

	public String getStart() {
		return start == null ? "0" : start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getLimit() {
		return limit == null ? String.valueOf(IConstant.PAGE_LIMIT) : limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}


}
